Table of Contents Previous Section

Creating Objects

There are two different ways to create objects in WebScript.The first approach, which applies to all classes, is to use class creation methods. The second approach applies to just NSStrings, NSArrays, and NSDictionaries. For these classes WebScript provides a convenient syntax for initializing constant objects.

Creating Constant NSStrings, NSArrays, and NSDictionaries

NSStrings, NSArrays, and NSDictionaries are the classes you use most often in WebScript.WebScript provides a convenient syntax for initializing constant objects of these types. In such an assignment statement, the value you're assigning to the constant object is preceded by an at sign (@). You use parentheses to enclose the elements of an NSArray, and curly braces to enclose the key-value pairs of an NSDictionary. The following are examples of how you use this syntax to assign values to constant NSStrings, NSArrays, and NSDictionaries in WebScript:

myString = @"hello world";
myArray = @("hello", "goodbye");
myDictionary = @{"key" = 16};
anotherArray = @(1, 2, 3, "hello");
aDict = @{ "a" = 1; "b" = "hello world"; "c" = (1,2,3); "d" = { "x" = 1; "r" = 2 }};

The following rules apply when you use this syntax to create constant objects:

For more information on NSStrings, NSDictionaries, and NSArrays, see A Foundation for WebScript Programmers: Quick Guide to Useful Classes

Using Creation Methods

All classes provide creation methods that you can use to create an instance of that class. Depending on the class and the particular creation method, the instances of the class you create might either be mutable (modifiable) or immutable (constant). When you use creation methods to create NSStrings, NSArrays, and NSDictionaries, you can choose to create either an immutable or a mutable object. For clarity, it's best to use immutable objects wherever possible. Only use a mutable object if you need to change its value after you initialize it.

Here are some examples of using creation methods to create mutable and immutable NSString, NSArray, and NSDictionary objects:

// Create a mutable string
string = [NSMutableString stringWithFormat:@"The string is %@", aString];

// Create an immutable string
string = [NSString stringWithFormat:@"The string is %@", aString];

// Create a mutable array
array = [NSMutableArray array];
anotherArray = [NSMutableArray arrayWithObjects:@"Marsha", @"Greg", @"Cindy", nil];

// Create an immutable array
array = [NSArray arrayWithObjects:@"Bobby", @"Jan", @"Peter", nil];

// Create a mutable dictionary
dictionary = [NSMutableDictionary dictionary];

// Create an immutable dictionary
id stooges = [NSDictionary
    dictionaryWithObjects:@("Mo", "Larry", "Curley")
    forKeys:@("Stooge1", "Stooge2", "Stooge3")];

The instances of some classes, like NSCalendarDate, are always mutable. The following statements show examples of how you can create and modify NSCalendarDates:

// Using the creation method date, create an NSCalendarDate instance 
// 'now' that contains the current date and time
now = [NSCalendarDate date];

// Return a string representation of 'now' using a format string
dateString = [now descriptionWithCalendarFormat:@"%B %d, %Y"];

// Using the creation method dateWithString:, create an NSCalendarDate
// instance 'newDate' from 'dateString'
newDate = [NSCalendarDate dateWithString:dateString 
  calendarFormat:@"%B %d, %Y"];
  
// modify 'newDate' by decrementing its day field
newDate = [newDate addYear:0 month:0 day:-1 hour:0 minute:0 second:0];

For a detailed discussion of these classes and a more complete listing of methods, see A Foundation for WebScript Programmers: Quick Guide to Useful Classes.

Table of Contents Next Section